const path = require('path');
const ExtractPlugin = require('extract-text-webpack-plugin');
const HtmlPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const {readFileSync} = require('fs');
module.exports = {
    entry:'./src/js/index.js',
    // entry:{
    //     index:'./src/js/index.js',
    //     detail:'./src/js/detail.js'
    // },
    output:{
        path:path.join(__dirname,'./dist'),
        filename:'./js/[name].js'
    },
    mode:'development',
    module:{
        rules:[
            {
                test:/\.scss$/,
                loader:['style-loader','css-loader','sass-loader']
                // use:ExtractPlugin.extract({
                //     fallback:'style-loader', //
                //     use:['css-loader','sass-loader']
                // })
            },
            {
               test:/\.css$/,
               use:['style-loader','css-loader'] 
            },
            {
                test:/\.js$/,
                exclude:/node_modules/, //排除
                // include: //包含
                use:'babel-loader'
            },
            {
                test:/\.(eot|svg|ttf|woff)$/,
                use:'url-loader'
            },
            {
                test:/\.(png|jpg|gif|jpeg)$/,
                use:{
                    loader:'url-loader',
                    options:{
                        limit:10, // 1024B = 1KB  1024KB = 1MB  1024MB = 1GB
                        name:'imgs/[name].[ext]',
                        publicPath:'/'
                    }
                }
            }
        ]
    },
    plugins:[
        // new ExtractPlugin({
        //     filename:'./css/index.css'
        // }),
        new HtmlPlugin({
            // filename:'index1.html',
            template:'./index.html',
            // inject:false //是否注入
        }),
        new webpack.HotModuleReplacementPlugin()
    ],
    devServer:{
        port:9090,
        before(app){
            app.get('/api/list',(req,res) => {
                let list = readFileSync('./mock/list.json','utf-8');
                res.end(list)
            })
        },
        hot:true
    },
    resolve:{
        alias:{
            "indexScss$":path.join(__dirname,'./src/scss/index.scss')
        },
        extensions:[".scss",".js"]
    }
}